package com.mt.roll.report.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mt.roll.core.api.Assert;
import com.mt.roll.core.base.BaseService;
import com.mt.roll.core.dal.entity.PrintModel;
import com.mt.roll.core.dal.service.PrintModelServiceBiz;
import com.mt.roll.core.params.EnabledParam;
import com.mt.roll.core.params.IdListParam;
import com.mt.roll.core.params.IdParam;
import com.mt.roll.core.vo.IdListVo;
import com.mt.roll.core.vo.IdVo;
import com.mt.roll.core.vo.PageVo;
import com.mt.roll.core.vo.R;
import com.mt.roll.report.mapper.PrintModelVoMapper;
import com.mt.roll.report.params.PrintModelParam;
import com.mt.roll.report.params.PrintModelQueryParam;
import com.mt.roll.report.service.PrintModelService;
import com.mt.roll.report.vo.PrintModelVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 标签模板服务实现类
 *
 * @author hy
 * @date 2021/03/06
 * @since 1.0
 */
@Service
@Transactional
public class PrintModelServiceImpl extends BaseService implements PrintModelService {

    @Resource
    private PrintModelVoMapper printModelVoMapper;

    @Resource
    private PrintModelServiceBiz printModelServiceBiz;

    /**
     * 获取获取标签模板详情
     *
     * @param idParam 主键参数
     * @return printModelVo
     */
    @Override
    public R<PrintModelVo> get(IdParam idParam) {
        PrintModelVo printModelVo = printModelVoMapper.get(idParam);
        return successful(printModelVo);
    }

    /**
     * 获取xx列表
     *
     * @param printModelQueryParam xx查询参数
     * @return xx列表
     */
    @Override
    public R<List<PrintModelVo>> list(PrintModelQueryParam printModelQueryParam) {
        return null;
    }

    /**
     * 获取标签模板分页列表
     *
     * @param printModelQueryParam 标签模板查询参数
     * @return 标签模板分页列表
     */
    @Override
    public R<PageVo<PrintModelVo>> page(PrintModelQueryParam printModelQueryParam) {
        IPage<PrintModelVo> page = printModelVoMapper.page(new Page<>(printModelQueryParam.getCurrent(), printModelQueryParam.getSize()), printModelQueryParam);
        return successful(new PageVo<>(page,PrintModelVo.class));
    }

    /**
     * 获取xx树列表
     *
     * @param printModelQueryParam xx查询参数
     * @return xx树列表
     */
    @Override
    public R<List<PrintModelVo>> tree(PrintModelQueryParam printModelQueryParam) {
        return null;
    }

    /**
     * 新增标签模板
     *
     * @param printModelParam 标签模板参数
     * @return 主键
     */
    @Override
    public R<IdVo> add(PrintModelParam printModelParam) {
        PrintModel printModel = new PrintModel();
        BeanUtils.copyProperties(printModelParam,printModel);
        setCommonField(printModel);
        printModelServiceBiz.save(printModel);
        return successful(IdVo.builder().id(printModel.getId()).build());
    }

    /**
     * 更新标签模板
     *
     * @param printModelParam 更新标签模板参数
     * @return 主键
     */
    @Override
    public R<IdVo> update(PrintModelParam printModelParam) {
        PrintModel printModel = printModelServiceBiz.getById(printModelParam.getId());
        if (!printModel.getName().equals(printModelParam.getName())) {
            QueryWrapper<PrintModel> wrapper = new QueryWrapper<>();
            wrapper.eq(PrintModel.NAME, printModelParam.getName());
            Assert.eqZero(printModelServiceBiz.count(wrapper), "名称已存在");
        }
        if(!printModel.getCode().equals(printModelParam.getCode())){
            QueryWrapper<PrintModel> wrapper=new QueryWrapper<>();
            wrapper.eq(PrintModel.CODE,printModelParam.getCode());
            Assert.eqZero(printModelServiceBiz.count(wrapper),"编码已存在");
        }
        PrintModel updatePrintModel = new PrintModel();
        BeanUtils.copyProperties(printModelParam, updatePrintModel);
        setUpdateCommonField(updatePrintModel);
        printModelServiceBiz.updateById(updatePrintModel);
        return successful(IdVo.builder().id(printModel.getId()).build());
    }

    /**
     * 启用停用
     *
     * @param enabledParam 启停参数
     * @return 主键
     */
    @Override
    public R<IdVo> enabled(EnabledParam enabledParam) {
        return null;
    }

    /**
     * 删除标签模板
     *
     * @param idParam 主键参数
     * @return 主键
     */
    @Override
    public R<IdVo> delete(IdParam idParam) {
        PrintModel printModel = printModelServiceBiz.getById(idParam.getId());
        Assert.notNull("标签模板不存在", printModel);

        printModelServiceBiz.removeById(idParam.getId());
        return successful(IdVo.builder().id(printModel.getId()).build());
    }

    /**
     * 批量删除xx
     *
     * @param idListParam 主键列表参数
     * @return 主键列表
     */
    @Override
    public R<IdListVo> delete(IdListParam idListParam) {
        return null;
    }
}
